<template>
  <!-- 交易查询 -->
  <div class="ContactPerson" >
    <div class="searchNav">
      <!-- <h2>联系人</h2> -->
      <!--   <el-button class="button" icon="el-icon-plus" type="primary" style="margin-left: 0"
        @click="handleAdd">新增</el-button> -->
      <div class="block">
        <el-date-picker
          v-model="range"
          type="daterange"
          style="width: 250px; margin-left: 10px"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          @change="changeTime"
        >
        </el-date-picker>
      </div>

      <div class="searchParams">
        <el-input
          class="input"
          v-model="searchKeyword.Keyword"
          placeholder="请输入赊货方/销售方"
          clearable
          @keydown.enter.native="getDataList"
        ></el-input>
        <el-button type="primary" class="button" @click="search"
          >查询</el-button
        >
        <el-button class="button" @click="reset('reset')">重置</el-button>
      </div>
      <el-tag
        type="success"
        size="medium"
        effect="plain"
        style="font-size: 17px; font-weight: bold"
        >赊货总计<span style="margin-right: 5px"
          >:{{ toThousands(summaryTotalData[0].shehuoTotalMoney)  }}</span
        ></el-tag
      >
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        style="width: 100%"
        :max-height="height"
        show-summary
        :summary-method="getSummaries"
      >
        <!-- <el-table-column prop="cCusName" label="银行名称"></el-table-column> -->
        <el-table-column prop="BuyerName" label="赊货方">
          <template slot-scope="scope">
            <!-- <span @click="detailPage(scope.row)">{{scope.row.cCusName}}</span> -->
            <el-link @click="handlelDetail(scope.row)">{{
              scope.row.BuyerName
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="CompanyName" label="销售方"> </el-table-column>
        <el-table-column prop="CreateTime" label="赊货发起日期">
          <template slot-scope="scope">
            <span>{{
              scope.row.CreateTime ? scope.row.CreateTime.substring(0, 10) : ""
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="SignTime" label="货品交付日期">
          <template slot-scope="scope">
            <span>{{
              scope.row.SignTime ? scope.row.SignTime.substring(0, 10) : ""
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="ReceiveSum" label="赊货金额">
          <template slot-scope="scope">
            <span>{{ toThousands(scope.row.TotalSum) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="IsLoan" label="是否放款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.IsLoan==true" >已放款</el-tag>
            <el-tag v-else type="danger">待放款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="255" align="center">
          <template slot-scope="scope">
            <!-- <el-button type="text" @click="handleEdit(scope.row)">修改</el-button> -->
            <!-- <el-button type="text"  @click="handleDelete(scope.row)" style="color:red">删除</el-button> -->
            <el-button
              type="text"
              style="color: #409eff"
              @click="detailPage(scope.row.Id)"
              >明细</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 底部统计 -->
      <!-- <summaryTableTotalVue :summary-total-data="summaryTotalData" :table-column-data="tableColumnData"
				ref="summaryTableTotalVue" :parentObj="{ ...this }"></summaryTableTotalVue> -->

      <!-- 分页 -->
      <div style="display: flex; justify-content: flex-end; margin-top: 8px">
        <pagination
          :total="pagination.total"
          :page.sync="pagination.PageIndex"
          :limit.sync="pagination.PageRows"
          @pagination="handlePageChange"
        />
      </div>
    </div>
    <!-- 明细页面 -->
    <TransactionInquiryDetail
      ref="TransactionInquiryDetail"
      :parentObj="{ ...this }"
    />

    <!-- 资质详情 -->
    <MyMerchantDetail ref="MyMerchantDetail" :parentObj="{ ...this }" />
  </div>
</template>

<script>
// 引入公共工具js文件
import radioGroup from "@/components/CompanyRadioGroup.vue";
import { toThousands } from "@/utils/Tools";
import TransactionInquiryDetail from "./TransactionInquiryDetail.vue";
// 引入表格统计组件
import summaryTableTotalVue from "../../components/summaryTableTotal.vue";
// 引入资质详情组件 统一用的是我的商户页面里的资质详情
import MyMerchantDetail from "../MyMerchant/MyMerchantDetail.vue";
export default {
  name: "transactionQuery",
  props: {},
  components: {
    TransactionInquiryDetail,
    radioGroup,
    summaryTableTotalVue,
    MyMerchantDetail,
  },

  computed: {
    height() {
      return document.documentElement.clientHeight * 0.75;
    },
  },
  data() {
    return {
      // 整体页面加载
      fullscreenLoading:false,
      // 时间范围
      range: [],
      // 格式化函数
      toThousands,
      pagination: {
        current: 1,
        pageSize: 25,
        total: 0,
        pageSizeOptions: [25, 50, 100],
      },
      sorter: {
        field: "QIANTIME",
        order: "desc",
      },
      // 表格底部统计列表列数
      tableColumnData: [
        {
          prop: "summaryTotalText",
          label: "总计",
        },
        {
          prop: "",
          label: "",
        },
        {
          prop: "",
          label: "",
        },
        {
          prop: "",
          label: "",
        },
        {
          prop: "shehuoMoney",
          label: "",
        },
        {
          prop: "",
          label: "",
        },
      ],
      summaryTotalData: [
        {
          summaryTotalText: "总计",
          shehuoTotalMoney: 0,
          shehuoMoney: 0,
        },
      ],
      tableData: [],
      tableDataObj: {
        // 赊货方
        BuyerName: "",
        // 销售方
        CompanyName: "",
        // 赊货发起日期
        CreateTime: "",
        // 货品交付日期
        SignTime: "",
        // 赊货金额
        shehuoMoney: 0,
        // 操作
        center: "",
      },
      queryParam: {
        CompanyId: "1647861082737348608",
        CustomerName: "",
      },
      loading: false,
      // 详情页显示
      drawer: false,
      // 备注
      form: {
        remark: "",
        userName: "",
        money: "",
        creditTime: "",
      },
      shehuoList: [],
      // 搜索关键字
      searchKeyword: {
        Keyword: "",
        StartDate: "",
        EndDate: "",
      },
    };
  },
  mounted() {
    // 获取交易查询列表数据
    this.getDataList();
  },
  created() {},
  methods: {
    //自定义方法
    getSummaries(param) {
      const { columns, data } = param;
      const sums = {};
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (index === 4) {
          sums[index] =  this.toThousands(this.tableDataObj.shehuoMoney) ;
          return;
        }
        // const values = data.map(item => Number(item[column.property]));
        // if (column.label == "水份含量(%)" || column.label == "原料重量(kg)") {
        //   if (!values.every(value => isNaN(value))) {
        //       sums[index] = values.reduce((prev, curr) => {
        //           const value = Number(curr);
        //           if (!isNaN(value)) {
        //               return prev + curr;
        //           } else {
        //               return prev;
        //           }
        //       }, 0);
        //       // sums[index] += ' kg';
        //   } else {
        //       // sums[index] = 'N/A';
        //   }
        // }
      });
      return sums;
    },
    // 获取交易查询顶部统计金额数据
    GetTotalTransaction(data) {
      this.$http
        .post("/Base/Bank_Info/GetTotalTransaction", {
          Search: {
            userids: data,
            Keyword: this.searchKeyword.Keyword,
            StartDate: this.searchKeyword.StartDate,
            EndDate: this.searchKeyword.EndDate,
          },
        })
        .then((resJson) => {
          if (resJson.Success) {
            this.summaryTotalData[0].shehuoTotalMoney =
              resJson.Data.CreditAmount;
          }
        });
    },
    // 获取底部统计金额数据 分页
    GetTotalBtmCredit(data) {
      this.$http
        .post("/Base/Bank_Info/GetTotalBtmTransaction", {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: "CreateTime",
          SortType: "desc",
          Search: {
            userids: data,
            Keyword: this.searchKeyword.Keyword,
            StartDate: this.searchKeyword.StartDate,
            EndDate: this.searchKeyword.EndDate,
          },
        })
        .then((resJson) => {
          if (resJson.Success) {
            // this.summaryTotalData[0].shehuoMoney = resJson.Data.CreditAmount
            this.tableDataObj.shehuoMoney = resJson.Data.CreditAmount;
          }
        });
    },
    // 商铺资质详情跳转
    handlelDetail(row) {
      if (row.BuyerStoreId) {
        this.$refs.MyMerchantDetail.openForm(row.BuyerStoreId);
      } else {
        this.$message.error("没有店铺信息");
      }
    },
    // 开始结束时间选择
    changeTime(e) {
      this.range = e;
      console.log("开始时间", e, this.queryParam);
      this.searchKeyword.StartDate = this.range[0];
      this.searchKeyword.EndDate = this.range[1];
      // 刷新放款列表
      this.tableData = [];
      this.pagination.current = 1;
      this.getDataList();
    },
    // 参数重置
    reset(event) {
      if (event === "reset") {
        this.range = [];
        this.searchKeyword.StartDate = "";
        this.searchKeyword.EndDate = "";
      }
      this.tableData = [];
      this.pagination.current = 1;
      this.getDataList();
    },
    // 搜索
    search() {
      this.reset();
    },
    // 新增交易查询
    handleAdd() {
      this.$refs.editForm.openForm();
    },
    //修改
    handleEdit(row) {
      this.$refs.editForm.openForm(row.Id);
    },
    // 删除
    handleDelete(row) {
      this.$message.success("已删除");
    },
    // 查看明细
    orderDetails(row) {
      this.$message.success("明细页面");
    },
    // 分页函数
    handlePageChange(data) {
      this.pagination.current = data.pageIndex;
      this.pagination.pageSize = data.pageSize;
      this.getDataList(); //这是重新获取修改日志列表
    },

    // 查询用户--
    CurrentUser() {
      return this.tableData.filter((item, index) => {
        return item.cCusName == this.queryParam.keyword;
      });
    },
    // 查看当前用户详情
    detailPage(row) {
      this.$refs.TransactionInquiryDetail.openForm(row);
    },
    //  获取交易查询列表数据
    getDataList() {
      let userId = JSON.parse(sessionStorage.getItem("userInfo"));
      this.$http
        .post(`/Base/Bank_Info/Getuserids?Id=${[userId.Id]}`)
        .then((res) => {
          this.userIdList = res.Data;
          if (res.Success) {
            this.TradinglistData(res.Data);
            // 获取交易查询顶部总金额统计
            this.GetTotalTransaction(res.Data);
            // 获取底部统计金额数据 分页
            this.GetTotalBtmCredit(res.Data);
          }
        });
    },
    // 获取交易查询列表数据
    TradinglistData(data) {
      this.tableDataObj = {
        // 赊货方
        BuyerName: "总计",
        // 销售方
        CompanyName: "",
        // 赊货发起日期
        CreateTime: "",
        // 货品交付日期
        SignTime: "",
        // 赊货金额
        shehuoMoney: 0,
        // 操作
        action: "action",
      };

      var that = this
      //创建loading对象开始遮罩
      this.loading = true;
      this.$http
        .post("/Base/Bank_Info/TransactionInquiry", {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: "CreateTime",
          SortType: "desc",
          Search: {
            userids: data,
            Keyword: this.searchKeyword.Keyword,
            StartDate: this.searchKeyword.StartDate,
            EndDate: this.searchKeyword.EndDate,
          },
        })
        .then((resJson) => {
          if (resJson.Success) {
            //请求结束关闭loading
            setTimeout(function () {
            that.loading = false;
          }, 600);
            this.tableData = resJson.Data;
            // this.tableData.push(this.tableDataObj)
            if (this.tableData) {
              const pagination = {
                ...this.pagination,
              };
              pagination.total = resJson.Total;
              this.pagination = pagination;
            }
          }
        });
    },
  },
  watch: {},
};
</script>

<style lang="less" scoped>

.searchNav .searchParams {
  justify-content: left;
}

.inputItem {
  width: 80%;
}

::v-deep .el-form-item__label {
  font-size: 16px;
  font-weight: bold;
  margin-left: 20px;
}

.particulars {
  margin-left: 20px;
}
</style>
